package cn.iocoder.yudao.module.myerp.controller.admin.SPUproduct;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.myerp.controller.admin.SPUproduct.vo.*;
import cn.iocoder.yudao.module.myerp.dal.dataobject.SPUproduct.ErpProductSpuDO;
import cn.iocoder.yudao.module.myerp.service.SPUproduct.ErpProductSpuService;

@Tag(name = "管理后台 - ERP SPU级产品")
@RestController
@RequestMapping("/myerp/erp-product-spu")
@Validated
public class ErpProductSpuController {

    @Resource
    private ErpProductSpuService erpProductSpuService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP SPU级产品")
    @PreAuthorize("@ss.hasPermission('myerp:erp-product-spu:create')")
    public CommonResult<Long> createErpProductSpu(@Valid @RequestBody ErpProductSpuSaveReqVO createReqVO) {
        return success(erpProductSpuService.createErpProductSpu(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP SPU级产品")
    @PreAuthorize("@ss.hasPermission('myerp:erp-product-spu:update')")
    public CommonResult<Boolean> updateErpProductSpu(@Valid @RequestBody ErpProductSpuSaveReqVO updateReqVO) {
        erpProductSpuService.updateErpProductSpu(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP SPU级产品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('myerp:erp-product-spu:delete')")
    public CommonResult<Boolean> deleteErpProductSpu(@RequestParam("id") Long id) {
        erpProductSpuService.deleteErpProductSpu(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP SPU级产品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('myerp:erp-product-spu:query')")
    public CommonResult<ErpProductSpuRespVO> getErpProductSpu(@RequestParam("id") Long id) {
        ErpProductSpuDO erpProductSpu = erpProductSpuService.getErpProductSpu(id);
        return success(BeanUtils.toBean(erpProductSpu, ErpProductSpuRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP SPU级产品分页")
    @PreAuthorize("@ss.hasPermission('myerp:erp-product-spu:query')")
    public CommonResult<PageResult<ErpProductSpuRespVO>> getErpProductSpuPage(@Valid ErpProductSpuPageReqVO pageReqVO) {
        PageResult<ErpProductSpuDO> pageResult = erpProductSpuService.getErpProductSpuPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpProductSpuRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP SPU级产品 Excel")
    @PreAuthorize("@ss.hasPermission('myerp:erp-product-spu:export')")
    @OperateLog(type = EXPORT)
    public void exportErpProductSpuExcel(@Valid ErpProductSpuPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpProductSpuDO> list = erpProductSpuService.getErpProductSpuPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP SPU级产品.xls", "数据", ErpProductSpuRespVO.class,
                        BeanUtils.toBean(list, ErpProductSpuRespVO.class));
    }

}